package com.luoy.order;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 实体类须实现此接口
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderBean implements WritableComparable<OrderBean> {
	/**
	 * 订单id
	 */
	private String orderId;
	/**
	 * 订单价格
	 */
	private Double price;

	//序列化方法
	@Override
	public void write(DataOutput out) throws IOException {
		//str类型用
		out.writeUTF(orderId);
		out.writeDouble(price);
	}

	//反序列化方法
	@Override
	public void readFields(DataInput in) throws IOException {
		//读取str类型
		this.orderId = in.readUTF();
		this.price = in.readDouble();

	}

	@Override
	public int compareTo(OrderBean o) {
		//两次排序

		//先按照id号排序
		int comResult = this.orderId.compareTo(o.getOrderId());
		
		if (comResult == 0) {
			//按照价格倒序排序
			comResult = this.price > o.getPrice()?-1:1;
		}
		
		return comResult;
	}

	@Override
	public String toString() {
		return orderId + "\t" + price;
	}
}
